package com.example.a2302fx.register

import androidx.lifecycle.viewModelScope
import com.example.base.entity.CodeEntity
import com.example.base.entity.LoginEntity
import com.example.base.mvvm.BaseViewModel
import com.example.base.mvvm.Res
import com.example.base.net.SingleLiveData
import kotlinx.coroutines.launch

class RegisterViewModel:BaseViewModel() {

    val codeSuccess = SingleLiveData<Res<CodeEntity>>()
    val codeFail = SingleLiveData<String>()
    fun code(phone:String){
        viewModelScope.launch {
            try {
                codeSuccess.postValue(apiService.code(phone))
            }catch (e:Exception){
                codeFail.postValue(e.message.toString())
            }
        }
    }

    val regSuccess = SingleLiveData<Res<LoginEntity>>()
    val regFail = SingleLiveData<String>()
    fun register(captcha:String,phone:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["captcha"] = captcha
                map["phone"] = phone
                regSuccess.postValue(apiService.register(mapToBody(map)))
            }catch (e:Exception){
                regFail.postValue(e.message.toString())
            }
        }
    }
}